package top.swimmer.tokenizer.dictionary;

import top.swimmer.tokenizer.dictionary.trie.DoubleArrayTrie;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoyou on 16-12-11.
 */
public class T_DoubleArrayTrie {
    public static void main(String[] args) throws Exception {
        DoubleArrayTrie trie1 = initWordsDictionary2("/home/xiaoyou/IdeaProjects/swimmer/swimmer-tokenizer/src/test/resources/dictionary/dic.txt");
    }

    private static DoubleArrayTrie initWordsDictionary2(String filePath) throws Exception {
        DoubleArrayTrie trie = new DoubleArrayTrie();
        BufferedReader in = null;
        File file = new File("/home/xiaoyou/IdeaProjects/swimmer/swimmer-tokenizer/src/main/resources/dictionary/dic.txt");
        in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        List<String> words = new ArrayList<>();
        String line;
        while ((line = in.readLine()) != null) {
            words.add(line);
        }
        Long start = System.currentTimeMillis();
        trie.build(words);
        System.out.println("加载词库： " + (System.currentTimeMillis() - start));

        Long start1 = System.currentTimeMillis();
        for (int i = 0; i < words.size(); i++) {
            if (!trie.contains(words.get(i))) {
                System.out.println(words.get(i) + " -> " + trie.contains(words.get(i)));
            }
        }
        System.out.println("查找词库： " + (System.currentTimeMillis() - start1));
        System.out.println("的发达 -> " + trie.contains("龟龟琐琐龟龟琐琐龟龟琐琐龟龟琐琐"));
        return trie;
    }

    public void main1(String[] args) {
        List<String> words = new ArrayList<String>() {{
            add("我们");
            add("我们都有一个家");
            add("都有一个家");
            add("我是好人");
            add("都是好人");
            add("你们");
        }};

        DoubleArrayTrie trie = new DoubleArrayTrie();
        Long start = System.currentTimeMillis();
        trie.build(words);
        System.out.println(System.currentTimeMillis() - start);
        System.out.println(trie.contains("我们"));
        System.out.println(trie.contains("我们都有一个家", 2, 5));
        System.out.println(trie.contains("我们都有一个家", 0, 2));
        System.out.println(trie.contains("我们都有一个家", 0, 3));
        System.out.println(trie.contains("都有一个家"));
        System.out.println(trie.contains("我是好人"));
        System.out.println(trie.contains("都是好人"));
        System.out.println(trie.contains("你们"));
        System.out.println(trie.contains("我们都是好人"));
    }
}
